//
//  KKSearchBar.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/20.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

class KKSearchBar: UIView {
    
    // 增加可视化属性
//    @IBInspectable var cornerRadius: CGFloat {
//        
//        get {
//            
//            return layer.cornerRadius
//        }
//        
//        set {
//            
//            self.layer.cornerRadius = newValue
//        }
//    }

    @IBOutlet weak var searchTF: UITextField!
    @IBOutlet weak var rightConstraint: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!
    class func loadSearchBar() -> KKSearchBar {
        
        let searchView = NSBundle.mainBundle().loadNibNamed("KKSearchBar", owner: nil, options: nil).last as! KKSearchBar
        
        return searchView
    }
    
    // 初始化一些设置
    override func awakeFromNib() {
        
        self.bounds.size.width = ScreenWidth
        
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        searchTF.leftViewMode = .Always
        
        searchTF.leftView?.frame = CGRectMake(0, 0, 50, 40)
        searchTF.leftView?.contentMode = .Center
        
        // 设置圆角和颜色
//        searchTF.layer.cornerRadius = 20
//        searchTF.layer.masksToBounds = true
////
//        searchTF.layer.borderColor = UIColor.orangeColor().CGColor
//        searchTF.layer.borderWidth = 2
        
//        
//        self.layer.cornerRadius = 20
//        self.layer.masksToBounds = true
//        self.layer.borderColor = UIColor.orangeColor().CGColor
//        self.layer.borderWidth = 2
    }
    
    @IBAction func cancelBtnDidClick(sender: AnyObject) {
        
        
        UIView.animateWithDuration(0.25) {
            
           self.rightConstraint.constant = 0
            
           self.layoutIfNeeded()
            
        }
        
        self.endEditing(true)
    }
    
    @IBAction func textFieldBeginEditing(sender: AnyObject) {
        
        UIView.animateWithDuration(0.25) { 
            
          self.rightConstraint.constant = self.cancelBtn.bounds.width
           
            self.layoutIfNeeded()
        }
        
        
    }
    
}
